<?php
include_once('inc/functions.inc.php');

if(!isEmpty(getParam('operation'))){
    
    switch(getParam('operation')){
        case 'bancos': {
            fntBancosGetBancos($db);
            break;
        }
        case 's': {
            fntBancosGrabar($db);
            break;
        }
    }
    die();
}

function fntBancosGetBancos($db){

    $strQuery = "SELECT * FROM cllg_bnc_banco ORDER BY nombre";
    $qTMP = $db->query($strQuery);
    $intNumRows = $db->num_rows;
    
    if( $intNumRows > 0 ) {
        ?>
        <select name="sltBancosBanco">
            <option value="0">--- Nuevo banco ---</option>
            <?php
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                ?>
                <option value="<?php print $rTMP["ID"]; ?>"><?php print $rTMP["NOMBRE"]; ?></option>
                <?php    
            }
            ?>            
        </select>
        <?php
    }
    else {
        ?>
        <select name="sltBancosBanco">
            <option value="0">--- Nuevo banco ---</option>
        </select>       
        <?php        
    }
    $db->free_result($qTMP);
    
    ?>
    <script type="text/javascript" language="javascript">
    $("select[name='sltBancosBanco']").change(function() {
        $("input[name='txtBancosNombre']").val($("select[name='sltBancosBanco'] option:selected").text());
    });
    </script>
    <?php
    
}
function fntBancosGrabar($db){

    $id = getParam("sltBancosBanco");
    $nombre = getParam("txtBancosNombre");
    
    if( $id == 0 ) {    
        $strQuery = "SELECT * FROM cllg_bnc_banco WHERE nombre = '{$nombre}'";
        $qTMP = $db->query($strQuery);
        $intNumRows = $db->num_rows;
        $db->free_result($qTMP);
        if( $intNumRows == 0 ) {
            $db->query("INSERT INTO cllg_bnc_banco (nombre) VALUES ('{$nombre}')");
            ?>
            <div class="ui-widget" style="border: 0px solid white; width: 900px; height: auto; vertical-align: bottom; text-align: center;">
                <div class="ui-state-highlight ui-corner-all" style="margin-top: 0px; padding: 0pt 0.7em; height: auto; width: 220px;"> 
                    <span class="ui-icon ui-icon-info" style="float: left; margin-right: 0.3em;"></span>
                    Datos guardados.
               </div>
            </div>
            <br>
            <?php
        }
        else {
            ?>
            <div class="ui-widget" style="border: 0px solid white; width: 900px; height: auto; vertical-align: bottom; text-align: center;">
                <div class="ui-state-error ui-corner-all" style="margin-top: 0px; padding: 0pt 0.7em; height: auto; width: 220px;"> 
                    <span class="ui-icon ui-icon-alert" style="float: left; margin-right: 0.3em;"></span>
                    Ya existe banco.
               </div>
            </div>
            <br>        
            <?php        
        }
    }
    else {
        $db->query("UPDATE cllg_bnc_banco SET nombre = '{$nombre}' WHERE id = '{$id}'");
        ?>
        <div class="ui-widget" style="border: 0px solid white; width: 900px; height: auto; vertical-align: bottom; text-align: center;">
            <div class="ui-state-highlight ui-corner-all" style="margin-top: 0px; padding: 0pt 0.7em; height: auto; width: 220px;"> 
                <span class="ui-icon ui-icon-info" style="float: left; margin-right: 0.3em;"></span>
                Datos actualizados.
           </div>
        </div>
        <br>
        <?php
    }
    
}
?>